package com.example.myapplication.util;
import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnect {
    private final static String DIVER = "com.mysql.cj.jdbc.Driver";
    private final static String URL = "jdbc:mysql://localhost:3306/test_db_springboot?useSSL=false&autoReconnect=true&characterEncoding=utf8";
    private final static String USERNAME = "root";
    private final static String PASSWORD = "Lichuandi0911";

    // 数据库连接
//    public static java.sql.Connection getConn() throws Exception {
//        Class.forName(DIVER);
//        return DriverManager.getConnection(URL, USERNAME, PASSWORD);

    // 静态代码块加载驱动
    static {
        try {
            Class.forName(DIVER);
        } catch (ClassNotFoundException e) {
            System.out.println("驱动加载失败!");
            System.out.println(e.getMessage());
//            e.printStackTrace();
        }
    }
    //
    public Connection getConnection() {
        Connection conn = null;
        try {
            conn =  DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (Exception e) {
            System.out.println("数据库连接失败!");
            System.out.println(e.getMessage());
        }
        return conn;
    }
}
